package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.OnTabSelectedListener;
import com.sgb.kjwl.databinding.NeedActivityBinding;
import com.sgb.kjwl.view.ui.adapter.home.ViewPagerAdapter;
import com.sgb.kjwl.view.ui.fragment.enterprise.NeedFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:张磊
 * 日期:2020/12/21 0021
 * 说明:需求大厅
 */
public class NeedActivity extends AppActivity {

    private NeedActivityBinding mBinding;
    private int mIndex;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, NeedActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }
    private TextView selectTextView;

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.need_activity);
        mBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (selectTextView == null)
                    selectTextView = new TextView(getActivity());
                selectTextView.setTextSize(15);
                selectTextView.setTextColor(getResources().getColor(R.color.color_main));
                selectTextView.setText(tab.getText());
                selectTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                tab.setCustomView(selectTextView);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        initTab(new String[]{"商标需求", "专利需求", "版权需求", "服务需求"});
    }


    private void initTab(String[] titls) {
//        List<Pair<String, Fragment>> mList = new ArrayList<>();
//        for (int i = 0; i < titls.length; i++) {
//            String s = titls[i];
//            Bundle bundle = new Bundle();
//            NeedFragment couponFragment = new NeedFragment();
//            bundle.putString("data", s);
////            bundle.putInt("type", i+1);
//            couponFragment.setArguments(bundle);
//            mList.add(new Pair<>(s, couponFragment));
//        }
//
//        mBinding.vpNeed.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), mList));
//        mBinding.vpNeed.setOffscreenPageLimit(titls.length);
//        mBinding.tab.setupWithViewPager(mBinding.vpNeed);
        String categoryMark = null, categoryPatent = null, categoryCopyright = null, categoryCompany = null, categoryService = null, categoryProperty = null;
        String category = getIntent().getStringExtra("category");
        int position = getIntent().getIntExtra("position", 0);
        if (!TextUtils.isEmpty(category)) {
            switch (position) {
                case 0:
                    categoryMark = category;
                    break;
                case 1:
                    categoryPatent = category;
                    break;
                case 2:
                    categoryCopyright = category;
                    break;
                case 3:
                    categoryCompany = category;
                    break;
                case 4:
                    categoryService = category;
                    break;
                case 5:
                    categoryProperty = category;
                    break;
            }
        }
        List<Pair<String, Fragment>> mList = new ArrayList<>();
        mList.add(new Pair<>(titls[0], NeedFragment.newInstance(titls[0], categoryMark)));
        mList.add(new Pair<>(titls[1], NeedFragment.newInstance(titls[1], categoryPatent)));
        mList.add(new Pair<>(titls[2], NeedFragment.newInstance(titls[2], categoryCopyright)));
//        mList.add(new Pair<>(titls[3], NeedFragment.newInstance(titls[3], categoryCompany)));
        mList.add(new Pair<>(titls[3], NeedFragment.newInstance(titls[3], categoryService)));
//        mList.add(new Pair<>(titls[5], NeedFragment.newInstance(titls[5], categoryProperty)));
//        for (String s : titls) {
//            Bundle bundle = new Bundle();
//            NeedFragment couponFragment = new NeedFragment();
//            bundle.putString("data", s);
//            couponFragment.setArguments(bundle);
//            mList.add(new Pair<>(s, couponFragment));
//        }
        mBinding.vpNeed.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), mList));
        mBinding.vpNeed.setOffscreenPageLimit(titls.length);
        mBinding.tab.setupWithViewPager(mBinding.vpNeed);
        mBinding.tab.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mIndex = tab.getPosition();
            }
        });
        mBinding.tab.getTabAt(0).select();
        mBinding.vpNeed.setCurrentItem(position);
    }

}